package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.deuwise.common.base.BuildTree;
import com.deuwise.common.base.Tree;

import com.deuwise.system.entity.DictDo;
import com.deuwise.system.mapper.DictDoMapper;
import com.deuwise.system.service.DictDoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Sysdict 接口实现类
 *@author deuwise
 *@date 2018-11-12 16:05:19
 **/
@Service
public class DictDoServiceImpl extends ServiceImpl<DictDoMapper, DictDo> implements DictDoService {

    Logger logger= LoggerFactory.getLogger(DictDoServiceImpl.class);


    @Override
    public Integer queryByName(String name, DictDo dictDo) {
        Wrapper<DictDo> dictDoEntityWrapper=new EntityWrapper<>();
        dictDoEntityWrapper.eq("name",name);
        if(dictDo!=null){
            if(StringUtils.isNotBlank(dictDo.getId())) {
                dictDoEntityWrapper.ne("id", dictDo.getId());
            }
           if(StringUtils.isNotBlank(dictDo.getParentId())) {
                dictDoEntityWrapper.eq("parent_id", dictDo.getParentId());
            }
        }
        return baseMapper.selectCount(dictDoEntityWrapper);
    }

    List<DictDo> getAllDict(List<DictDo> allList, String deptId){
        List<DictDo> deptDOList=new ArrayList<>();
        List<DictDo> childList=   allList.stream().filter(f->f.getParentId().equals(deptId)).collect(Collectors.toList());
        deptDOList.addAll(childList);
        if(childList!=null&&childList.size()>0){
            for(DictDo deptDO:childList){
                deptDOList.addAll(getAllDict(allList,deptDO.getId()));
            }
        }
        return deptDOList;

    }

    @Override
    public Tree<DictDo> getTree(DictDo dictDo) {
        List<Tree<DictDo>> trees = new ArrayList<Tree<DictDo>>();
        Wrapper<DictDo> wrapper=new EntityWrapper<>();
        wrapper.orderBy("order_num",false).orderBy("id",false);
       /* wrapper.eq("deleted",0);*/
        if(StringUtils.isNotBlank(dictDo.getName())){
            wrapper.like("name",dictDo.getName());
        }
        List<DictDo> sysDepts=null;
        if(StringUtils.isNotBlank(dictDo.getParentId())){
            if(dictDo.getParentId().equals("0")){
                wrapper = new EntityWrapper<>();
                // wrapper.eq("deleted",0);
                List<DictDo> allList = baseMapper.selectList(wrapper);
                sysDepts = getAllDict(allList, dictDo.getParentId());
            }else {
                wrapper = new EntityWrapper<>();
                // wrapper.eq("deleted",0);
                List<DictDo> allList = baseMapper.selectList(wrapper);
                Optional<DictDo> deptD1 = allList.stream().filter(f -> f.getId().equals(dictDo.getParentId())).findFirst();
                if (deptD1.isPresent()) {
                    sysDepts = getAllDict(allList, dictDo.getParentId());
                    sysDepts.add(deptD1.get());
                    if (StringUtils.isNotBlank(dictDo.getName())) {
                        if (sysDepts != null && sysDepts.size() > 0) {
                            sysDepts = sysDepts.stream().filter(f -> f.getName().contains(dictDo.getName())).collect(Collectors.toList());

                        }
                    }

                }
            }
        }else {
            sysDepts = baseMapper.selectList(wrapper);
        }
        for (DictDo sysDept : sysDepts) {
            Tree<DictDo> tree = new Tree<DictDo>();
            tree.setId(sysDept.getId().toString());
            if(StringUtils.isNotBlank(sysDept.getParentId())) {
                tree.setParentId(sysDept.getParentId().toString());
            }else {
                tree.setParentId("");
            }
            Map<String, Object> map=new HashMap<>(16);
            map.put("order_num",sysDept.getOrderNum());
            map.put("deleted",sysDept.getDeleted());
            // map.put("userId",sysDept.getUserId());
            tree.setAttributes(map);
            tree.setText(sysDept.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<DictDo> t = BuildTree.build(trees);
        return t;
    }
}